﻿using EMSystem.WebSite.Filter;
using prince.CSSDemo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EMSystem.EF;
using System.Data.Entity;
using System.Collections;

namespace EMSystem.WebSite.Controllers
{
    public class HomeController : BaseController
    {
        protected readonly string AppSessionKey = "UserApp";

        [CheckLoginState]
        public ActionResult Index()
        {
            List<Menu> list = new List<Menu>()
            {
                new Menu()
                {
                    FunID=1000,
                    FunName="角色管理",
                    FunTypeID=1,
                    FunURL="",
                    ParentFun=0,
                    MenuOrder=1
                },
                 new Menu()
                {
                    FunID=1001,
                    FunName="锁定玩家",
                    FunTypeID=1,
                    FunURL="锁定玩家",
                    ParentFun=1000,
                    MenuOrder=1
                },
                 new Menu()
                {
                    FunID=1002,
                    FunName="解锁玩家",
                    FunTypeID=1,
                    FunURL="解锁玩家",
                    ParentFun=1000,
                    MenuOrder=2
                },
                 new Menu()
                {
                    FunID=1003,
                    FunName="系统管理",
                    FunTypeID=1,
                    FunURL="系统管理",
                    ParentFun=0,
                    MenuOrder=2
                }
                ,
                 new Menu()
                {
                    FunID=1004,
                    FunName="公告管理",
                    FunTypeID=1,
                    FunURL="公告管理",
                    ParentFun=1003,
                    MenuOrder=1
                },
                 new Menu()
                {
                    FunID=1005,
                    FunName="实时数据查看",
                    FunTypeID=1,
                    FunURL="实时数据查看",
                    ParentFun=0,
                    MenuOrder=3
                }
                ,
                 new Menu()
                {
                    FunID=1006,
                    FunName="指标数据",
                    FunTypeID=1,
                    FunURL="指标数据",
                    ParentFun=1005,
                    MenuOrder=1
                }
                ,
                 new Menu()
                {
                    FunID=1007,
                    FunName="邮件",
                    FunTypeID=1,
                    FunURL="邮件",
                    ParentFun=0,
                    MenuOrder=4
                }
                ,
                 new Menu()
                {
                    FunID=1008,
                    FunName="发送邮件",
                    FunTypeID=1,
                    FunURL="发送邮件",
                    ParentFun=1007,
                    MenuOrder=1
                }
                ,
                 new Menu()
                {
                    FunID=1009,
                    FunName="邮件历史",
                    FunTypeID=1,
                    FunURL="邮件历史",
                    ParentFun=1007,
                    MenuOrder=2
                }
            };

            return View(list);
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        [HttpGet]
        public ActionResult Login()
        {
            if (Session[AppSessionKey] != null)
            {
                //var model = Session[AppSessionKey];
                return RedirectToAction("Index");
            }
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Login(string loginname, string password, int typeid)
        {
            int count = 0;
            switch (typeid)
            {
                case 0:
                    var predicate = PredicateBuilder.True<Student>();
                    predicate = predicate.AndAlso(p => p.stuID == loginname && p.stuPwd == password);
                    count = dbContext.Student.Where(predicate).ToList().Count();
                    if (count > 0)
                    {
                        Hashtable hh = new Hashtable();
                        hh.Add(typeid, dbContext.Student.Where(predicate).FirstOrDefault());
                        Session[AppSessionKey] = hh;
                    }
                    break;
                case 1:
                    var predicate1 = PredicateBuilder.True<Zhuren>();
                    predicate1 = predicate1.AndAlso(p => p.zhurenID == loginname && p.zhurenPwd == password);
                    count = dbContext.Zhuren.Where(predicate1).ToList().Count();
                    if (count > 0)
                    {
                        Hashtable hh = new Hashtable();
                        hh.Add(typeid, dbContext.Zhuren.Where(predicate1).FirstOrDefault());
                        Session[AppSessionKey] = hh;
                    }

                    break;
                case 2:
                    var predicate2 = PredicateBuilder.True<Admin>();
                    predicate2 = predicate2.AndAlso(p => p.adminName == loginname && p.adminPwd == password);
                    count = dbContext.Admin.Where(predicate2).ToList().Count();
                    if (count > 0)
                    {
                        Hashtable hh = new Hashtable();
                        hh.Add(typeid, dbContext.Admin.Where(predicate2).FirstOrDefault());
                        Session[AppSessionKey] = hh;
                    }
                    break;
            }
            if (count == 0)
            {
                return Json(100, "没有匹配的信息,登录失败");
            }
            return Json(typeid, "匹配成功");
        }

        //public ActionResult TestJson()
        //{
        //    return GetJson("~/Content/json/menu.json");
        //}
    }
}